package org.mall.modules.order.service.impl;

import org.mall.common.constant.OrderCommentStatusEnum;
import org.mall.common.constant.OrderReplyUserTypeEnum;
import org.mall.common.constant.OrderStatusEnum;
import org.mall.convert.order.OrderCommentConvert;
import org.mall.modules.order.entity.OrderCommentDO;
import org.mall.modules.order.entity.OrderCommentReplyDO;
import org.mall.modules.order.mapper.OrderCommentMapper;
import org.mall.modules.order.mapper.OrderCommentReplayMapper;
import org.mall.modules.order.mapper.OrderMapper;
import org.mall.modules.order.service.OrderCommentService;
import org.mall.modules.pojo.order.bo.OrderCommentInfoBO;
import org.mall.modules.pojo.order.bo.OrderCommentPageBO;
import org.mall.modules.pojo.order.bo.OrderCommentStateInfoPageBO;
import org.mall.modules.pojo.order.bo.OrderCommentTimeOutBO;
import org.mall.modules.pojo.order.dto.OrderCommentCreateDTO;
import org.mall.modules.pojo.order.dto.OrderCommentPageDTO;
import org.mall.modules.pojo.order.dto.OrderCommentStateInfoPageDTO;
import org.mall.modules.pojo.order.dto.OrderCommentTimeOutPageDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 订单评论 service impl
 *
 * @author wtz
 * @time 2019
 */
@Service
public class OrderCommentServiceImpl implements OrderCommentService {

    @Autowired
    private OrderCommentMapper orderCommentMapper;

    @Autowired
    private OrderCommentReplayMapper orderCommentReplayMapper;
    @Autowired
    private OrderMapper orderMapper;


    @Override
    public Boolean createOrderComment(List<OrderCommentCreateDTO> orderCommentCreateDTOs) {
        List<OrderCommentDO>list=orderCommentCreateDTOs.stream().map(o->{
                    OrderCommentDO orderCommentDO= OrderCommentConvert.INSTANCE.convertOrderCommentDO(o);
                            orderCommentDO.setCreateTime(new Date());orderCommentDO.setUpdateTime(new Date());
                    return orderCommentDO ;}
            ).collect(Collectors.toList());
        orderCommentMapper.insertList(list);
        orderMapper.updateOrderStatus(list.get(0).getOrderId(), OrderStatusEnum.COMMENT_COMPLETE.getValue());
        return true;
    }

    @Override
    public OrderCommentPageBO getOrderCommentPage(OrderCommentPageDTO orderCommentPageDTO) {
        OrderCommentPageBO orderCommentPageBO=new OrderCommentPageBO();
        //分页内容
        List<OrderCommentDO> orderCommentDOList=orderCommentMapper.selectCommentPage(orderCommentPageDTO);
        //分页评论的 id
        List<Integer> commentIds=orderCommentDOList.stream().map(x->x.getId()).collect(Collectors.toList());
        //获取商家最新的评论回复

        List<OrderCommentReplyDO> temp;
        if (commentIds.size()>0) {
         temp   =orderCommentReplayMapper.selectCommentNewMerchantReplyByCommentIds(commentIds,
                    OrderReplyUserTypeEnum.MERCHANT.getValue());

        }
        else {
            temp=new ArrayList<>();
        }
        List<OrderCommentReplyDO> orderCommentReplyDOList =temp;
            //评论组装
        List<OrderCommentPageBO.OrderCommentItem> orderCommentItemList=orderCommentDOList.stream()
                .map(x->{

                    OrderCommentPageBO.OrderCommentItem o=new OrderCommentPageBO.OrderCommentItem();
                    BeanUtils.copyProperties(x,o);
                    orderCommentReplyDOList.stream().forEach(a->{if (a.getCommentId().equals(o.getId())){
                        o.setReplyContent(a.getReplyContent());
                    }});
                    return o;
                        }
                ).collect(Collectors.toList());
        //总数
        int totalCount=orderCommentMapper.selectCommentTotalCountByProductSpuId(orderCommentPageDTO.getProductSpuId());
        orderCommentPageBO.setOrderCommentItems(orderCommentItemList);
        orderCommentPageBO.setTotal(totalCount);
        return orderCommentPageBO;
    }


    @Override
    public OrderCommentInfoBO getOrderCommentInfo(Integer commentId) {
        //查询评论详情
        OrderCommentDO orderCommentDO=orderCommentMapper.selectCommentInfoByCommentId(commentId);
        return OrderCommentConvert.INSTANCE.convertOrderCommentInfoBO(orderCommentDO);
    }

    @Override
    public OrderCommentStateInfoPageBO getOrderCommentStateInfoPage(OrderCommentStateInfoPageDTO orderCommentStateInfoPageDTO) {
        OrderCommentStateInfoPageBO orderCommentStateInfoPageBO=new OrderCommentStateInfoPageBO();
        //总数
        int total=orderCommentMapper.selectOrderCommentStateInfoTotal(orderCommentStateInfoPageDTO.getUserId(),
                orderCommentStateInfoPageDTO.getCommentState());
        //查询评论状态详情
        List<OrderCommentDO> orderCommentDOList=orderCommentMapper.selectOrderCommentStateInfoPage(orderCommentStateInfoPageDTO);
        //转化评论状态详情
        List<OrderCommentStateInfoPageBO.OrderCommentStateInfoItem> orderCommentStateInfoItemList=
                OrderCommentConvert.INSTANCE.convertOrderCommentStateInfoItems(orderCommentDOList);
        orderCommentStateInfoPageBO.setTotal(total);
        orderCommentStateInfoPageBO.setOrderCommentStateInfoItems(orderCommentStateInfoItemList);
        return orderCommentStateInfoPageBO;
    }

    @Override
    public List<OrderCommentTimeOutBO> getOrderCommentTimeOutPage(OrderCommentTimeOutPageDTO orderCommentTimeOutPageDTO) {
        List<OrderCommentDO> orderCommentDOList=orderCommentMapper.selectOrderCommentTimeOutPage(orderCommentTimeOutPageDTO);
        return OrderCommentConvert.INSTANCE.convertOrderCommentTimeOutBOList(orderCommentDOList);
    }

    @Override
    public void updateBatchOrderCommentState(List<OrderCommentTimeOutBO> orderCommentTimeOutBOList) {
        orderCommentMapper.updateBatchOrderCommentState(OrderCommentStatusEnum.SUCCESS_COMMENT.getValue(),orderCommentTimeOutBOList);
    }
}
